﻿    var _roleId = 0;
    $(document).ready(function () {
        GetModuleRights();
    });

    function saveRole() {
        var params = "{'role':'" + $("[id$='txtRoleName']").val() + "'}";
        $.ajax({
            url: "/UserManagement/RoleManager.aspx/SaveRole",
            type: "POST",
            dataType: "json",
            data: params,
            contentType: "application/json; charset=utf-8"
        }).done(function (data) {
            showSuccessMsg("Role saved successfully.");
            $("#tbodyRoles").html("");
            var roles = data.d;
            for (var b = 0; b < roles.length; b++) {
                var html = "<tr>";
                html += "<td>" + roles[b].RoleName + "</td>";
                html += "</tr>";
                $("#tbodyRoles").append(html);
            }
        });
    }
    function saveRoleRights() {        
        var roleRights = new Array();
        $("#tbodyModuleRights tr.rights").each(function () {            
            if ($(this).find(".moduleRight").is(":checked")) {
                var objRoleRights = new Object();
                objRoleRights.MRId = $(this).find(".moduleRight").val();
                objRoleRights.RoleId = _roleId;
                roleRights.push(objRoleRights);
            }
        });

        $.ajax({
            url: "/UserManagement/RoleManager.aspx/SaveRoleRights",
            type: "POST",
            dataType: "json",
            data: JSON.stringify({ ListRoleRight: roleRights, RoleId: _roleId }),
            contentType: "application/json; charset=utf-8"
        }).done(function (data) {
            showSuccessMsg("Role rights saved successfully.");

        });
    }
    function GetRoleRights() {
        var params = "{'RoleId':'" + _roleId + "'}";
        $.ajax({
            url: "../UserManagement/RoleManager.aspx/GetRoleRights",
            type: "POST",
            dataType: "json",
            data: params,
            contentType: "application/json; charset=utf-8"
        }).done(function (data) {
            var result = data.d;
            for (var r = 0 ; r < result.length; r++) {
                var rightId = $.trim(result[r].MRId);
                $("#chkRight_" + rightId).prop("checked", true);
            }
        });
    }
    function roleClick(elem) {
        _roleId = $(elem).find(".roleId").val();
        GetRoleRights();
    }
    function GetModuleRights() {
        $.ajax({
            url: "../UserManagement/RoleManager.aspx/GetModuleRights",
            type: "POST",
            dataType: "json",
            data: {},
            contentType: "application/json; charset=utf-8"
        }).done(function (data) {

            var result = JSON.parse(data.d);
            result = result["$values"][0];

            var moduleMain = result.ListModuleMain.$values;
            var rightsHtml = "";
            for (var m = 0; m < moduleMain.length; m++) {
                var moduleSub = moduleMain[m].ModuleSubs.$values;

                rightsHtml += "<tr>";
                rightsHtml += "<td><div style='padding-left: 10px;'>";
                rightsHtml += "<div style='color: maroon;font-weight: bold;border-bottom:solid 1px #ccc;'><input type='checkbox' class='mainModule' id='chkMain_" + m + "' onclick='mainModuleClick(this)' /><label for='chkMain_" + m + "' style='padding-left: 10px;'>" + moduleMain[m].ModuleName + "</label></div>";
                
                // Bind Sub Module

                rightsHtml += "<div  style='padding-left: 20px;'>";
                for (var ms = 0; ms < moduleSub.length; ms++) {
                    var moduleRights = moduleSub[ms].ModuleRights.$values;
                    rightsHtml += "<div  style='color: lightseagreen;font-weight: bold;border-bottom:solid 1px #ccc;'><input type='checkbox' class='subModule'  id='chkSub_" + ms + "' onclick='subModuleClick(this)' /><label for='chkSub_" + ms + "' style='padding-left: 10px;'>" + moduleSub[ms].ModuleName + "</label></div>";

                    // Bind Rights                    
                    rightsHtml += "<div  style='padding-left: 10px;'><table style='width: 100%;'>";
                    for (var mr = 0; mr < moduleRights.length; mr++) {
                        rightsHtml += "<tr class='rights'>";
                        rightsHtml += "<td style='border-bottom:solid 1px #ccc;padding-left: 20px;'><input type='checkbox' value='" + moduleRights[mr].MRId + "' class='moduleRight' id='chkRight_" + moduleRights[mr].MRId + "'  onclick='moduleRightClick(this)' /><label for='chkRight_" + moduleRights[mr].MRId + "' style='padding-left: 10px;'>" + moduleRights[mr].RightName + "</label></td>";
                        rightsHtml += "</tr>";
                    }
                    rightsHtml += "</table></div>";
                }
                rightsHtml += "</div></div></td>";
                rightsHtml += "</tr>";
            }
            $("#tbodyModuleRights").html(rightsHtml);
        });
    }
    function mainModuleClick(elem) {
        if ($(elem).is(":checked")) {
            $("#tbodyModuleRights").find("input[type='checkbox']").prop("checked", true);
        }
        else {
            $("#tbodyModuleRights").find("input[type='checkbox']").prop("checked", false);
        }
    }
    function subModuleClick(elem) {
        if ($(elem).is(":checked")) {
            $("#tbodyModuleRights").find("input[type='checkbox']").prop("checked", true);
        }
        else {
            $("#tbodyModuleRights").find("input[type='checkbox']").prop("checked", false);
        }
    }
    function moduleRightClick(elem) {

    }